package com.leilingmin.www.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.leilingmin.www.config.LeilingminInfo;
import com.leilingmin.www.entity.Illness;
import com.leilingmin.www.entity.IllnessKind;
import com.leilingmin.www.entity.Medicine;
import com.leilingmin.www.entity.User;
import com.leilingmin.www.service.IllnessKindService;
import com.leilingmin.www.service.IllnessService;
import com.leilingmin.www.service.MedicineService;
import com.leilingmin.www.util.Assert;
import com.leilingmin.www.util.RespResult;
import com.leilingmin.www.vo.request.FeedBackRequestVo;
import com.leilingmin.www.vo.response.FeedBackResponseVo;
import com.leilingmin.www.vo.response.HashMapResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController extends BaseController{
    @Autowired
    private LeilingminInfo leilingminInfo;
    @Autowired
    private IllnessService illnessService;
    @Autowired
    private IllnessKindService illnessKindService;
    @Autowired
    private MedicineService medicineService;
    @RequestMapping("index")
    //参数非必填，内部实际转换为null，所以要求类型是引用类型【自定义数据类型，包装类】
    //不可以是基本类型
    //如何设置参数的必填
    //参数名称与前端一致？？a--->abc


    public String index(Model model, FeedBackRequestVo feedBackRequestVo){
        System.out.println("配置信息"+leilingminInfo);
        model.addAttribute("echo",leilingminInfo);
        System.out.println(feedBackRequestVo);
        System.out.println("表示GET请求/index的URL会到达");

        model.addAttribute("uesername","雷灵敏23311143");

        FeedBackResponseVo feedBackResponseVo=new FeedBackResponseVo();
        feedBackResponseVo.setName("雷灵敏");
        feedBackResponseVo.setEmail("123@qq.com");
        feedBackResponseVo.setTitle("留言标题");
        feedBackResponseVo.setContent("留言详细内容");
        model.addAttribute("feedBackInfo",feedBackResponseVo);
//
//        HashMap<String,String>hashMap= new HashMap<>();
//        hashMap.put("name","测试");
//        hashMap.put("email","123@qq.com");
//        hashMap.put("title","留言标题");
//        hashMap.put("content","留言详细内容");
//        Model.addAttribute("mapInfo",hashMap);
        HashMapResponseVo hashMapResponseVo=new HashMapResponseVo();
        hashMapResponseVo.put("测试Map","123@qq.com","留言标题","留言详细内容");
        model.addAttribute("mapInfo",hashMapResponseVo);
        return "index";
    }


    //查找疾病
    @RequestMapping("/findIllness")
    public String findIllness101(Model model,String illnessName, @RequestParam(defaultValue = "1")Integer page){
        System.out.println("表示GET请求/index的URL会到达");
        //
        IPage<Illness> dataByIllnessName = illnessService.getIllnessPageDataByIllnessName(illnessName,5,page);
        model.addAttribute("illness",dataByIllnessName.getRecords());
        return "search-illness";
    }
    //药品管理
    @RequestMapping("/all-medical")
    public String allMedical(Model model,String medicineName, @RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize) {
        List<Medicine> medicinePageDataByIllnessName = medicineService.findMedicineByName(medicineName,page,pageSize);
        model.addAttribute("medicines", medicinePageDataByIllnessName);
        return "all-medical";
    }
    @RequestMapping("/doctor")
    public String doctor(){
        System.out.println("表示GET请求/index的URL会到达");
        return "doctor";
    }
    @RequestMapping("/findMedicines")
    public String findMedicines(Model model,String nameValue,@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize){
        List<Medicine> medicineList= (List<Medicine>) medicineService.findMedicineByName(nameValue,currentPage,pageSize);
        model.addAttribute("medicineList",medicineList);
        System.out.println("表示GET请求/index的URL会到达");
        return "illness";
    }

    @RequestMapping("/feedback.html")
    public String feedback() {
        return "feedback";
    }
    @RequestMapping("/all-illness")
    public String allIllness() {
        return "all-illness";
    }


    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index";
    }
    @GetMapping("/profile")
    public String profile(HttpSession session){
        User loginUser=(User) session.getAttribute("loginUser");
        if(Assert.isEmpty(loginUser)){
            return "redirect:/index";
        }
        return "profile";
    }
    public RespResult reLoginTip() {
        return RespResult.fail("还未登录请登录！");
    }

    @ModelAttribute
    public void commonRes(Model model){
        model.addAttribute("name","雷灵敏");
    }

}
